<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Home extends MY_Controller
{

    /* 构造 */
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Permission');
    }

    /* 显示主页 */
    public function index()
    {
        $user = $this->session->user;
        $data['user'] = $user;
        $this->load->view('home/main', $data);
    }

    /* 显示菜单栏 */
    public function menu()
    {
        $data = array();
        $permission = $this->session->permission;
        $userPermission = $permission['permission_id'];
        $userPermission = explode(',', $userPermission);
        $result = $this->Permission->find();
        $tree = $this->getChild(0, $result, $userPermission);
        $data['tree'] = json_encode($tree);
        $data['permissionCode'] = implode(',',$this->session->permissionCode);
        $this->load->view('home/subMenu', $data);
    }

    /* 显示右边页卡项 */
    public function main()
    {
        $this->load->view('home/open');
    }

    /* 加载效果 */
    public function load()
    {
        $this->load->view('home/loading');
    }

    /* 添加空白页 */
    public function getDefault()
    {
    	$this->load->view('home/getOpen');
    }

    /* 获取子节点 */
    private function getChild($pid, $data, $userPermission)
    {
        $tree = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid && in_array($v['id'], $userPermission)) {
                unset($data[$k]);
                $tmp = array();
                $tmp['id'] = $v['id'];
                $tmp['parentId'] = $v['pid'];
                $tmp['name'] = $v['name'];
                $tmp['href'] = empty($v['controller']) ? '' : $v['controller'] . '/' . $v['action'];
                switch ($v['permission_type']) {
                    case 1:
                        $tmp['type'] = 'menu';
                        break;
                    case 2:
                        $tmp['type'] = 'button';
                        break;
                    default:
                        $tmp['type'] = '';
                }
                $tmp['children'] = $this->getChild($v['id'], $data, $userPermission);
                $tree[] = $tmp;
            }
        }
        return $tree;
    }
}

